package com.ruoyi.coffee.service.impl;

import java.util.List;
import java.util.Optional;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.coffee.vo.AuthUser;
import org.springframework.stereotype.Service;
import com.ruoyi.coffee.mapper.UsersMapper;
import com.ruoyi.coffee.domain.Users;
import com.ruoyi.coffee.service.IUsersService;

import javax.annotation.Resource;

/**
 * 咖啡微信用户Service业务层处理
 *
 * @author dxj
 * @date 2024-12-19
 */
@Service
public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users> implements IUsersService {

    @Resource
    private UsersMapper usersMapper;

    /**
     * 查询咖啡微信用户
     *
     * @param id 咖啡微信用户主键
     * @return 咖啡微信用户
     */
    @Override
    public Users selectUsersById(Long id) {
        return usersMapper.selectUsersById(id);
    }

    /**
     * 查询咖啡微信用户列表
     *
     * @param users 咖啡微信用户
     * @return 咖啡微信用户
     */
    @Override
    public List<Users> selectUsersList(Users users) {
        return usersMapper.selectUsersList(users);
    }

    /**
     * 新增咖啡微信用户
     *
     * @param users 咖啡微信用户
     * @return 结果
     */
    @Override
    public int insertUsers(Users users) {
        return usersMapper.insertUsers(users);
    }

    /**
     * 修改咖啡微信用户
     *
     * @param users 咖啡微信用户
     * @return 结果
     */
    @Override
    public int updateUsers(Users users) {
        return usersMapper.updateUsers(users);
    }

    /**
     * 批量删除咖啡微信用户
     *
     * @param ids 需要删除的咖啡微信用户主键
     * @return 结果
     */
    @Override
    public int deleteUsersByIds(Long[] ids) {
        return usersMapper.deleteUsersByIds(ids);
    }

    /**
     * 删除咖啡微信用户信息
     *
     * @param id 咖啡微信用户主键
     * @return 结果
     */
    @Override
    public int deleteUsersById(Long id) {
        return usersMapper.deleteUsersById(id);
    }

    /**
     * app端登录
     *
     * @param userName
     * @param password
     * @return
     */
    @Override
    public Optional<AuthUser> mbLogin(String userName, String password) {
        long ONE_DAY_MILLIS = 1000 * 60 * 60 * 24;
        LambdaQueryWrapper<Users> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.select(Users::getId, Users::getNickname, Users::getAvatarUrl);
        lambdaQueryWrapper.eq(Users::getUsername, userName);
        lambdaQueryWrapper.eq(Users::getPassword, password);
        // 查询用户
        Users user = usersMapper.selectOne(lambdaQueryWrapper);
        // 如果用户存在，构建 AuthUser 对象
        return Optional.ofNullable(user)
                .map(u -> AuthUser.builder()
                        .id(u.getId())
                        .username(u.getNickname())
                        .nickName(u.getNickname())
                        .avatarUrl(u.getAvatarUrl())
                        .loginTime(System.currentTimeMillis())
                        .expireTime(System.currentTimeMillis() + ONE_DAY_MILLIS)
                        .build());
    }
}

